* PANEL A OF TABLES 1 AND 2: DiD ESTIMATIONS COMPARING OW TO ALL OTHER CANTONS * 

/*
Tables of the paper: 
		- Table 1, Panel A (analysis at municipality level)
		- Table 2, Panel A (analysis at municipality level)
*/


// NOTE: data needed for this do-file cannot be shared due to privacy reasons

/* following packages need to be installed:
ssc install winsor2
ssc install reghdfe
ssc install boottest
ssc install estout
*/


version 16.1

clear all
cap log close 
cap clear matrix
set more off
cap set scheme mygraphs


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

*   LOAD & PREPARE THE DATA for analysis at municipality level, without controls
*	- columns 1-5 

run "$mypathRR/Resources/prep_municipality_level_data.do"

global yrs20012016 "year > 2000 & year < 2017"
global yrs19952016 "year > 1994 & year < 2017"
global yrsall "year =!."

* only years for which data on OW is available
drop if year < 1995
drop if (year == 1996 | year == 1998 | year == 1999 | year == 2000) 

* gen a cluster variable canton x year as suggested by referee
egen cantXyr_clust = group(cant year)
	// 459 unique values

// only look at those outcomes where parallel trends look ok according to ES-estimates
global outcomes "share300K reink reink_w reink_ctrl reink_tr reink_tr_w"

// m0: baseline (mun & time FE, weights, no clustering)
foreach outcome in $outcomes {
	
display " THIS IS OUTCOME `outcome'"
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(gemeinde year)
}
else {
		reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(gemeinde year)
}
eststo m0_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
		quiet reghdfe `outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if year > 1994 & year < 2017, absorb(gemeinde)
		}
		else {
		quiet reghdfe `outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if $yrs19952016 , absorb(gemeinde)
		}
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m0_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m0_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m0_`outcome' = boot_ci_hi

	

* same model with 2-way clustering
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(gemeinde year) vce(cluster cantXyr_clust gemeinde)
}
else {
		reghdfe `outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(gemeinde year) vce(cluster cantXyr_clust gemeinde)
}

tempname SE2W clCxY clGem
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCxY' 	= e(N_clust1)
scalar define `clGem' 	= e(N_clust2)

eststo m0_2w_`outcome'



// add stats to be passed on to estout
	estimates restore m0_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m0_`outcome'} 
	estadd scalar upper = ${hi_m0_`outcome'}
	estadd scalar clusters = 26
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m0_`outcome'
	
	
	estimates restore m0_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m0_2w_`outcome'

}

// m1: baseline (mun & time FE, weights, no clustering, residualized outcome)

foreach outcome in $outcomes {
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(gemeinde year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(gemeinde year)
}
eststo m1_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
		quiet reghdfe resid`outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if  year > 1994 & year < 2017, absorb(gemeinde)
		}
		else {
		quiet reghdfe resid`outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if  $yrs19952016 , absorb(gemeinde)
		}
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m1_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m1_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m1_`outcome' = boot_ci_hi
	
	
* same model with 2-way clustering	
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , absorb(gemeinde year) vce(cluster cantXyr_clust gemeinde)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , absorb(gemeinde year) vce(cluster cantXyr_clust gemeinde)
}

tempname SE2W clCxY clGem
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCxY' 	= e(N_clust1)
scalar define `clGem' 	= e(N_clust2)

eststo m1_2w_`outcome'


// add stats to be passed on to estout
	estimates restore m1_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m1_`outcome'} 
	estadd scalar upper = ${hi_m1_`outcome'}
	estadd scalar clusters = 459
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m1_`outcome'
	
	estimates restore m1_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m1_2w_`outcome'
	
}	

// m4: CLUSTER (mun & time FE, weights, clustered at canton level)
foreach outcome in $outcomes {
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , vce(cluster cant) absorb(gemeinde year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , vce(cluster cant) absorb(gemeinde year)
}
eststo m4_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
		quiet reghdfe resid`outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if  year > 1994 & year < 2017, absorb(gemeinde)
		}
		else {
		quiet reghdfe resid`outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if  $yrs19952016 , absorb(gemeinde)
		}
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m4_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m4_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m4_`outcome' = boot_ci_hi
		

	
* same model with 2-way clustering		
if "`outcome'" == "share300Kc" | "`outcome'" == "share300Kc2" {
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if year > 1994 & year < 2017 , vce(cluster cantXyr_clust gemeinde) absorb(gemeinde year)
}
else {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 , vce(cluster cantXyr_clust gemeinde) absorb(gemeinde year)
}

tempname SE2W clCxY clGem
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCxY' 	= e(N_clust1)
scalar define `clGem' 	= e(N_clust2)

eststo m4_2w_`outcome'


// add stats to be passed on to estout
	estimates restore m4_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m4_`outcome'} 
	estadd scalar upper = ${hi_m4_`outcome'}
	estadd scalar clusters = 459
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m4_`outcome'
	
	estimates restore m4_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m4_2w_`outcome'
	

}

// m4 up to 2008 only: CLUSTER (mun & time FE, weights, clustered at canton level)
foreach outcome in share200K share300K {
		reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 & year < 2008, vce(cluster cant) absorb(gemeinde year)
eststo m4_08_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		quiet reghdfe resid`outcome' Interaction treated period ib2005.year [aweight = weight_`outcome'] if  $yrs19952016 & year < 2008, absorb(gemeinde)
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m4_08_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m4_08_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m4_08_`outcome' = boot_ci_hi
		

* same model with 2-way clustering			
	reghdfe resid`outcome' Interaction treated period [aweight = weight_`outcome'] if $yrs19952016 & year < 2008, vce(cluster cantXyr_clust gemeinde) absorb(gemeinde year)

tempname SE2W clCxY clGem
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCxY' 	= e(N_clust1)
scalar define `clGem' 	= e(N_clust2)
		
eststo m4_08_2w_`outcome'

		
// add stats to be passed on to estout
	estimates restore m4_08_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m4_08_`outcome'} 
	estadd scalar upper = ${hi_m4_08_`outcome'}
	estadd scalar clusters = 459
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m4_08_`outcome'
	
	estimates restore m4_08_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m4_08_2w_`outcome'

}

*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

*   LOAD & PREPARE THE DATA for analysis at municipality level, with controls
*	- column 6

run "$mypathRR/Resources/prep_municipality_level_data_controls.do"

global yrs20012016 "year > 2000 & year < 2017"
global yrs19952016 "year > 1994 & year < 2017"
global yrsall "year =!."

* only years for which data on OW is available
drop if year < 1995
drop if (year == 1996 | year == 1998 | year == 1999 | year == 2000) 

* gen a cluster variable canton x year as suggested by referee
egen cantXyr_clust = group(cant year)
	// 459 unique values


// only look at those outcomes where parallel trends look ok according to ES-estimates
global outcomes "reink"

global controls " nowork retiree self married singlep marriedch "

foreach outcome in $outcomes {
// m4c: CLUSTER (mun & time FE, weights, clustered at canton level)
reghdfe resid`outcome' Interaction treated period $controls [aweight = weight_`outcome'] if $yrs19952016, vce(cluster cant) absorb(gemeinde year)
eststo m4c_`outcome'

		//Run boottest to get bootstrapped clustered pvals and CIs for all coefficients
		qui reghdfe resid`outcome' Interaction treated period $controls ib2005.period [aweight = weight_`outcome'] if $yrs19952016, vce(cluster cant) absorb(gemeinde)
		boottest Interaction, cluster(cant) boottype(wild) nograph seed(1002) reps(20000)
		estadd r(CI) : m4c_`outcome'
		* save lower and upper bounds as scalars / globals
		matrix define wild = r(CI)
		scalar define boot_ci_lo = wild[1,1]
		global lo_m4c_`outcome' = boot_ci_lo
		scalar define boot_ci_hi = wild[1,2]
		global hi_m4c_`outcome' = boot_ci_hi


		
* same model with 2-way clustering			
reghdfe resid`outcome' Interaction treated period $controls [aweight = weight_`outcome'] if $yrs19952016, vce(cluster cantXyr_clust gemeinde) absorb(gemeinde year)
eststo m4c_2w_`outcome'

tempname SE2W clCxY clGem
scalar define `SE2W' 	= _se[Interaction]
scalar define `clCxY' 	= e(N_clust1)
scalar define `clGem' 	= e(N_clust2)


// add stats to be passed on to estout
	estimates restore m4c_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar lower = ${lo_m4c_`outcome'} 
	estadd scalar upper = ${hi_m4c_`outcome'}
	estadd scalar clusters = 459
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m4c_`outcome'
	
	estimates restore m4c_2w_`outcome'
	summ `outcome' if e(sample)  & cant == 6 & year < 2006 [aweight = weight_`outcome']
	local average = r(mean)
	estadd scalar yavg = `average'
	estadd scalar se2w = `SE2W'
	estadd scalar clust1 = `clCxY'
	estadd scalar clust2 = `clGem'
	estimates store m4c_2w_`outcome'
}	




*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

// OUTPUT TABLES TO LATEX
cd "$mypathRR/Results/"


* TABLE 1, panel A with 2-way clustering
estout m0_2w_share300K m1_2w_share300K  using "Tab1_PanelA)-DID-share300K-2wclust.tex" 			///
, replace style(tex) cells( "b(star fmt(3))" ) 				///
keep(Interaction) varlabels(Interaction "DiD" ) 									///
stats(se2w N r2 clust1 clust2  , 										///
fmt(%9.2f   %9.0g  %9.2f %9.0f %9.0f ) layout("[@]" @ @ @ @  )			///
labels("2-way cluster SE"  "\addlinespace[1.5ex] N" "R$^2$" "\addlinespace[1.5ex] Canton-Year clusters" "Municipality clusters" )) ///
mlabels(none)  collabels(none)	


* TABLE 2, panel A with winsorized values and 2-way clustering
estout m0_2w_reink_w m1_2w_reink_w   m4_2w_reink_ctrl m4_2w_reink_tr_w m4c_2w_reink using "Tab2_PanelA)-DID-reink_winsor-2wclust.tex" 			///
, replace style(tex) cells( "b(star fmt(2))" ) 									///
keep(Interaction) varlabels(Interaction "DiD") 									///
stats(se2w N r2 clust1 clust2  , 										///
fmt(%9.2f   %9.0g  %9.2f %9.0f %9.0f ) layout("[@]" @ @ @ @  )			///
labels("2-way cluster SE"  "\addlinespace[1.5ex] N" "R$^2$" "\addlinespace[1.5ex] Canton-Year clusters" "Municipality clusters" )) ///
mlabels(none)  collabels(none)   						

 
						* * * * *  E N D  * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
